package com.project.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.RoomBean;
import com.project.bean.RoomFacilityBean;
import com.project.dao.IRoomDao;
import com.project.dao.IRoomFacilityDao;
import com.project.mapper.IRoomFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.service.IRoomFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class RoomFacilityServiceImpl implements IRoomFacilityService {
    @Autowired
    private IRoomMapper roomMapper;
    @Autowired
    private IRoomFacilityMapper rfSetMapper;
    @Override
    public List<RoomFacilityBean> findByRoomId(int roomId) {
        QueryWrapper<RoomFacilityBean> qw = new QueryWrapper<>();
        qw.eq("fk_roomId",roomId);
        return rfSetMapper.selectList(qw);
    }

    @Override
    public void addFacility(RoomFacilityBean roomFacility) {
        rfSetMapper.insert(roomFacility);
        RoomBean room = roomMapper.selectById(roomFacility.getRoomId());
        room.setState("设施损坏");
        roomMapper.updateById(room);
    }

    @Override
    public void update(Integer facilityId, Integer roomId) {
        //得到设施ID对应设施对象
        RoomFacilityBean rf = rfSetMapper.selectById(facilityId);
        rf.setState("已解决");
        rfSetMapper.updateById(rf);
        QueryWrapper<RoomFacilityBean> qw = new QueryWrapper<>();
        qw.eq("s_state","未解决");
        qw.eq("fk_roomId",roomId);

        Integer num = rfSetMapper.selectCount(qw);
        if(num == 0){
            RoomBean room = roomMapper.selectById(roomId);
            room.setState("正常");
            roomMapper.updateById(room);
        }
    }
}
